package com.mall.utils;

import com.mall.common.Constant;
import com.mall.pojo.Wares;
import com.mall.service.IWaresService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description 获取商品数量初始化
 * @ClassName QuantityInitialization
 * @Author cxx
 * @Date 2023/4/27 16:11
 * @Version 1.0
 */
@Component

public class InitDataUtil {
    @Resource
    private IWaresService waresService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    public void initData() {
        List<Wares> list = waresService.list(null);
        list.forEach(ware -> {
            //将商品数量初始化在Redis中
            redisTemplate.opsForValue().set(ware.getId().toString() + "_num", ware.getNum());
            redisTemplate.opsForValue().set(ware.getId().toString() + "_price", ware.getPrice());
            //初始化内存标记
            Constant.MEMORY_TAGS.put(ware.getId().toString(), false);
        });
    }
}
